package com.tj.collection.db.admin.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.common.old.BaseDAOImpl;
import com.tj.collection.db.admin.dao.AdminTokenDAO;
import com.tj.collection.db.admin.entity.AdminTokenEntity;
import com.tj.collection.db.admin.entity.AdminTokenEntityConditions;
import com.tj.collection.db.admin.dao.mp.AdminTokenEntityMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/10
 * Time: 下午5:27
 */
@Repository
public class AdminTokenDAOImpl extends BaseDAOImpl<AdminTokenEntity, Long, AdminTokenEntityConditions> implements AdminTokenDAO {

    @Autowired
    private AdminTokenEntityMapper adminTokenEntityMapper;

    @Autowired
    @Override
    public void setBaseMapper() {
        super.setBaseMapper(adminTokenEntityMapper);
    }


    @Override
    public AdminTokenEntity getByAdminId(Long adminId) {
        AdminTokenEntityConditions conditions = new AdminTokenEntityConditions();
        AdminTokenEntityConditions.Criteria criteria = conditions.createCriteria();

        criteria.andAdminIdEqualTo(adminId);

        List<AdminTokenEntity> adminTokenEntityList = adminTokenEntityMapper.selectByExample(conditions);
        if (CollectionUtils.isNotEmpty(adminTokenEntityList)) {
            return adminTokenEntityList.get(0);
        }

        return null;
    }

    @Override
    public AdminTokenEntity getByToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }

        AdminTokenEntityConditions conditions = new AdminTokenEntityConditions();
        AdminTokenEntityConditions.Criteria criteria = conditions.createCriteria();
        criteria.andAccessTokenEqualTo(token);
        criteria.andDeletedEqualTo(false);

        conditions.setPage(new Page(1));

        List<AdminTokenEntity> adminTokenEntityList = adminTokenEntityMapper.selectByExample(conditions);
        if (CollectionUtils.isNotEmpty(adminTokenEntityList)) {
            return adminTokenEntityList.get(0);
        }

        return null;
    }

    @Override
    public Integer deleteByAdminId(Long adminId) {
        AdminTokenEntityConditions conditions = new AdminTokenEntityConditions();
        AdminTokenEntityConditions.Criteria criteria = conditions.createCriteria();
        criteria.andAdminIdEqualTo(adminId);
        return adminTokenEntityMapper.deleteByExample(conditions);
    }
}
